package breeze.pixel.weather.apps_util.utils.appsettings.settingsview;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.yalantis.ucrop.UCrop;

import java.io.File;

import breeze.pixel.weather.App;
import breeze.pixel.weather.R;
import breeze.pixel.weather.apps_util.utils.AppToolUtil;
import breeze.pixel.weather.apps_util.utils.MToast;
import breeze.pixel.weather.apps_util.views.mtoolbar.MToolbar;
import breeze.pixel.weather.service.app_widget.WeatherUtils;

public class SetAppwidgetPicView extends AppCompatActivity implements View.OnClickListener {

    private ImageView widget_bg, widget2_bg;

    private static final int _REQUEST_PICK_PICTURE_CODE_0 = 0;

    private static final int _REQUEST_PICK_PICTURE_CODE_1 = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_appwidget_pic_view);
        //实现状态栏沉浸
        getWindow().getDecorView().setSystemUiVisibility(1280);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        init();
        initData();
    }

    public void init() {
        MToolbar toolbar = findViewById(R.id.AppToolBar);
        toolbar.setTitle("自定义桌面部件背景");
        toolbar.setNavigation(R.mipmap.icon_back, view -> finish());
        widget_bg = findViewById(R.id.wid_get_1_layout);
        widget_bg.setOnClickListener(this);
        widget2_bg = findViewById(R.id.wid_get_2_layout);
        widget2_bg.setOnClickListener(this);
    }

    public void initData() {
        File widget_background = new File(AppToolUtil.getAppWidgetBackgroundPic_1(this));
        File widget_background2 = new File(AppToolUtil.getAppWidgetBackgroundPic_2(this));
        if (widget_background.exists()) {
            widget_bg.setImageBitmap(BitmapFactory.decodeFile(widget_background.getAbsolutePath()));
        } else {
            widget_bg.setImageResource(R.drawable.cardview_tendp_white);
        }
        if (widget_background2.exists()) {
            widget2_bg.setImageBitmap(BitmapFactory.decodeFile(widget_background2.getAbsolutePath()));
        } else {
            widget2_bg.setImageResource(R.drawable.cardview_tendp_white);
        }
    }

    private void selectBitmap(int type) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(intent, type);
        } else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
            MToast.show(this,"没有储存权限");
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.wid_get_1_layout) {
            selectBitmap(_REQUEST_PICK_PICTURE_CODE_0);
        } else if (view.getId() == R.id.wid_get_2_layout) {
            selectBitmap(_REQUEST_PICK_PICTURE_CODE_1);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == _REQUEST_PICK_PICTURE_CODE_0 && resultCode == Activity.RESULT_OK) {
            //背景图片1
            UCrop.of(data.getData(), Uri.fromFile(new File(AppToolUtil.getAppWidgetBackgroundPic_1(App.getContext())))).withAspectRatio(600, 300).start(this);
        } else if (requestCode == _REQUEST_PICK_PICTURE_CODE_1 && resultCode == Activity.RESULT_OK) {
            UCrop.of(data.getData(), Uri.fromFile(new File(AppToolUtil.getAppWidgetBackgroundPic_2(App.getContext())))).withAspectRatio(480, 480).start(this);
        }
        initData();
        WeatherUtils.updateView(null);
        super.onActivityResult(requestCode, resultCode, data);
    }

}